package com.fzy.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/DoUpdateNewsServlet")
public class DoUpdateNewsServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final String DB_URL = "jdbc:mysql://localhost:3306/news?useUnicode=true&characterEncoding=utf-8";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "13100140892Fzy!";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");

        // 加载 MySQL JDBC 驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            out.println("加载数据库驱动失败: " + e.getMessage());
            return;
        }

        // 获取表单提交的数据和 id 参数
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String date = request.getParameter("date");
        String heatParam = request.getParameter("heat");
        String idParam = request.getParameter("id");

        if (title == null || author == null || date == null || heatParam == null || idParam == null) {
            out.println("表单数据不完整，请重新提交");
            return;
        }

        try {
            int heat = Integer.parseInt(heatParam);
            int id = Integer.parseInt(idParam);

            try (
                    Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
                    // 准备预编译的 SQL 更新语句
                    PreparedStatement pstmt = conn.prepareStatement("UPDATE news_table SET title = ?, author = ?, date = ?, heat = ? WHERE id = ?")
            ) {
                pstmt.setString(1, title);
                pstmt.setString(2, author);
                pstmt.setString(3, date);
                pstmt.setInt(4, heat);
                pstmt.setInt(5, id);

                // 执行更新操作
                int rowsAffected = pstmt.executeUpdate();

                if (rowsAffected > 0) {
                    out.println("新闻记录更新成功");
                    // 重定向到新闻列表页面
                    response.sendRedirect("newsList");
                } else {
                    out.println("新闻记录更新失败，请检查输入或联系管理员");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            out.println("数据库连接或更新出错: " + e.getMessage());
        } catch (NumberFormatException e) {
            out.println("热度或 ID 不是有效的整数，请重新输入");
        }
    }
}
